__all__ = [
    "And", "Or", "Not", "Xor", "Eqv", "imp",
    "extXor",
    "lowBit",
    "isPowerOfTwo", "modPowerOfTwo",
    "Abs",
    "bitmax", "bitmin",
    "isSameSign",
    "getBit", "setBit", "unsetBit", "flapBit",
]


def And(l: int, r: int) -> int: return l & r
def Or(l: int, r: int) -> int: return l | r
def Not(l: int) -> int: return ~ l
def Xor(l: int, r: int) -> int: return l ^ r
def Eqv(l: int, r: int) -> int: return Not(l ^ r)
def imp(l: int, r: int) -> int: return l & ~ r


def extXor(x, y) -> bool:
    """广义异或"""
    return x != y


def lowBit(n: int) -> int:
    """求n的二进制最低位"""
    return n & -n


def isPowerOfTwo(n: int) -> bool:
    """判断是2的整数次幂"""
    return n > 0 and (n & (n-1)) == 0


def modPowerOfTwo(n: int, bit: int) -> int:
    """对2的bit次幂取模"""
    return n & ((1 << (bit-1))-1)


def Abs(n: int) -> int:
    """取绝对值"""
    return (n ^ (n >> 31))-(n >> 31)


def bitmax(a: int, b: int) -> int:
    return b & ((a-b) >> 31) | a & (~(a-b) >> 31)


def bitmin(a: int, b: int) -> int:
    return a & ((a-b) >> 31) | b & (~(a-b) >> 31)


def isSameSign(a: int, b: int) -> bool:
    """判断两非零数符号是否相同"""
    return (a ^ b) >= 0


def getBit(n: int, bit: int) -> int:
    """获取n上的第bit位数"""
    return (n >> bit) & 1


def setBit(n: int, bit: int) -> int:
    """将n上的第bit位数设为1"""
    return n | (1 << bit)


def unsetBit(n: int, bit: int) -> int:
    """将n上的第bit位数设为0"""
    return n & ~(1 << bit)


def flapBit(n: int, bit: int) -> int:
    """将n上的第bit位数取反"""
    return n ^ (1 << bit)
